package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.service.HttpClientService;
import com.jt.util.CookieUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    private static final String JT_TICKET="JT_TICKET";
    @Autowired
    private HttpClientService httpClientService;
    @Autowired
    private JedisCluster jedisCluster;

    //为接口创建代理对象，之后通过rpc进行访问
    @Reference
    private DubboUserService userService;

    @RequestMapping("/{moduleName}")
    public String moduleName(@PathVariable String moduleName){

        return moduleName;
    }

    /**
     * 完成httpclient测试
     *
     *
     */
    @RequestMapping("findUserList")
    @ResponseBody
    public List<User> findUserList() throws IOException {
        return httpClientService.findUserList();

    }

    /**
     * 用户注册操作
     */
    @RequestMapping("/doRegister")
    @ResponseBody
    public SysResult saveUser(User user){
        userService.saveUser(user);
        return SysResult.success();
    }

    /**
     * 用户单点登录操作
     *
     * cookie数据存储
     *  1.获取用户名，密码进行校验
     *  2.获取后端密钥信息
     *  3.如果一切正常，将数据存储到cookie中
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public SysResult doLogin(User user, HttpServletResponse response){
        String ticket=userService.findUserByUP(user);
        if(!StringUtils.hasLength(ticket)){
            //数据为空，表示用户名、密码错误
            return SysResult.fail();
        }
        //将数据存储到cookie中
        Cookie cookie = new Cookie("JT_TICKET", ticket);
        cookie.setPath("/");//读取cookie权限的设定,根目录中的请求，读取cookie
        //cookie.setPath("/user");//url地址路径在 /user下时才能获取cookie信息
        cookie.setMaxAge(7*24*60*60);//设置有效期
        cookie.setDomain("jt.com");//设置共享,只要域名中有jt.com，则可以共享cookie
        response.addCookie(cookie);
        return SysResult.success();
    }

    /**
     * 用户退出操作
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
        String ticket= CookieUtil.getCookieValue(request, JT_TICKET);
        if(StringUtils.hasLength(ticket)){
            //删除redis
            jedisCluster.del(ticket);
            //清楚cookie中的信息
            CookieUtil.addCookie(response, JT_TICKET, "", "/", "jt.com", 0);
        }
        return "redirect:/";
        //获取cookie中的JT_TICKET值
//        Cookie[] cookies = request.getCookies();
//        if(cookies!=null&&cookies.length>0){
//            for (Cookie cookie : cookies) {
//                if(cookie.getName().equals(JT_TICKET)){
//                    String ticket = cookie.getValue();
//                    //删除redis中的ticket信息
//                    jedisCluster.del(ticket);
//                    //清楚cookie中的信息
//                    cookie.setMaxAge(0);//0表示立即删除  -1关闭浏览器后立即删除
//                    cookie.setPath("/");
//                    cookie.setDomain("jt.com");
//                    response.addCookie(cookie);
//
//                }
//
//            }
//        }

    }
}
